package net.huashitong.ssydt.provider.h5.currentpolitics.service;

import com.alibaba.fastjson.JSONObject;
import net.huashitong.ssydt.provider.h5.currentpolitics.dto.AccessTokenDTO;
import net.huashitong.ssydt.provider.h5.currentpolitics.dto.JsapiTicketDTO;
import net.huashitong.ssydt.provider.utils.StringUtils;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2018.01.25
 */
@Service
@Transactional
@ConfigurationProperties("net.huashitong.wx.h5")
public class WxServiceImpl implements WxService {

    private Logger logger = LoggerFactory.getLogger(WxService.class);

    private String appId;

    private String secret;

    private String baseUrl;

    @Override
    public AccessTokenDTO getAccessToken(String appid, String secret) throws IOException {
        String url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appId=%s&secret=%s", appid, secret);
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = client.newCall(request).execute();
        return JSONObject.parseObject(response.body().string(), AccessTokenDTO.class);
    }


    @Override
    public JsapiTicketDTO getJsapiTicket() throws IOException {
        AccessTokenDTO accessToken = this.getAccessToken(appId, secret);
        if (StringUtils.isNotBlank(accessToken.getErrmsg())) {
            logger.error("获取accessToken错误：[{}]", accessToken);
        }
        String url = String.format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi", accessToken.getAccess_token());
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = client.newCall(request).execute();
        JsapiTicketDTO dto = JSONObject.parseObject(response.body().string(), JsapiTicketDTO.class);
        if (StringUtils.isNotBlank(dto.getErrmsg())) {
            logger.error("获取JsapiTicket错误：[{}]", dto);
        }
        return dto;
    }

    @Override
    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    @Override
    public String getBaseUrl() {
        return baseUrl;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    @Override
    public String getUrl() {
        return getBaseUrl() + "/h5/index.html";
    }

}
